package web.fabrimind.tas.service.impl;

import java.util.List;
import web.fabrimind.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import web.fabrimind.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import web.fabrimind.tas.domain.MaterialOutboundApplyDetail;
import web.fabrimind.tas.mapper.MaterialOutboundApplyMapper;
import web.fabrimind.tas.domain.MaterialOutboundApply;
import web.fabrimind.tas.service.IMaterialOutboundApplyService;

/**
 * 物料出库申请Service业务层处理
 *
 * @author zhihui
 * @date 2025-04-04
 */
@Service
public class MaterialOutboundApplyServiceImpl implements IMaterialOutboundApplyService
{
    @Autowired
    private MaterialOutboundApplyMapper materialOutboundApplyMapper;

    /**
     * 查询物料出库申请
     *
     * @param applyId 物料出库申请主键
     * @return 物料出库申请
     */
    @Override
    public MaterialOutboundApply selectMaterialOutboundApplyByApplyId(Long applyId)
    {
        return materialOutboundApplyMapper.selectMaterialOutboundApplyByApplyId(applyId);
    }

    /**
     * 查询物料出库申请列表
     *
     * @param materialOutboundApply 物料出库申请
     * @return 物料出库申请
     */
    @Override
    public List<MaterialOutboundApply> selectMaterialOutboundApplyList(MaterialOutboundApply materialOutboundApply)
    {
        return materialOutboundApplyMapper.selectMaterialOutboundApplyList(materialOutboundApply);
    }

    /**
     * 新增物料出库申请
     *
     * @param materialOutboundApply 物料出库申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMaterialOutboundApply(MaterialOutboundApply materialOutboundApply)
    {
        materialOutboundApply.setCreateTime(DateUtils.getNowDate());
        int rows = materialOutboundApplyMapper.insertMaterialOutboundApply(materialOutboundApply);
        insertMaterialOutboundApplyDetail(materialOutboundApply);
        return rows;
    }

    /**
     * 修改物料出库申请
     *
     * @param materialOutboundApply 物料出库申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMaterialOutboundApply(MaterialOutboundApply materialOutboundApply)
    {
        materialOutboundApply.setUpdateTime(DateUtils.getNowDate());
        materialOutboundApplyMapper.deleteMaterialOutboundApplyDetailByApplyId(materialOutboundApply.getApplyId());
        insertMaterialOutboundApplyDetail(materialOutboundApply);
        return materialOutboundApplyMapper.updateMaterialOutboundApply(materialOutboundApply);
    }

    /**
     * 批量删除物料出库申请
     *
     * @param applyIds 需要删除的物料出库申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMaterialOutboundApplyByApplyIds(Long[] applyIds)
    {
        materialOutboundApplyMapper.deleteMaterialOutboundApplyDetailByApplyIds(applyIds);
        return materialOutboundApplyMapper.deleteMaterialOutboundApplyByApplyIds(applyIds);
    }

    /**
     * 删除物料出库申请信息
     *
     * @param applyId 物料出库申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMaterialOutboundApplyByApplyId(Long applyId)
    {
        materialOutboundApplyMapper.deleteMaterialOutboundApplyDetailByApplyId(applyId);
        return materialOutboundApplyMapper.deleteMaterialOutboundApplyByApplyId(applyId);
    }

    /**
     * 新增物料出库申请明细信息
     *
     * @param materialOutboundApply 物料出库申请对象
     */
    public void insertMaterialOutboundApplyDetail(MaterialOutboundApply materialOutboundApply)
    {
        List<MaterialOutboundApplyDetail> materialOutboundApplyDetailList = materialOutboundApply.getMaterialOutboundApplyDetailList();
        Long applyId = materialOutboundApply.getApplyId();
        if (StringUtils.isNotNull(materialOutboundApplyDetailList))
        {
            List<MaterialOutboundApplyDetail> list = new ArrayList<MaterialOutboundApplyDetail>();
            for (MaterialOutboundApplyDetail materialOutboundApplyDetail : materialOutboundApplyDetailList)
            {
                materialOutboundApplyDetail.setApplyId(applyId);
                list.add(materialOutboundApplyDetail);
            }
            if (list.size() > 0)
            {
                materialOutboundApplyMapper.batchMaterialOutboundApplyDetail(list);
            }
        }
    }
}
